/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-21
 * Time: 10:48
 */
public class test1 {
    /*
    NewCoder BM83 字符串变形
     */
    public String trans (String s, int n) {
        // write code here
        if(n==0){
            return s;
        }
        StringBuffer sb = new StringBuffer();
        // 1.将小写转为大写，大写转为小写
        for(int i=0;i<n;i++){
            if(s.charAt(i)>='A' && s.charAt(i)<='Z'){
                sb.append( (char)(s.charAt(i)-'A'+'a') );
            }else if(s.charAt(i)>='a' && s.charAt(i)<='z'){
                sb.append( (char)(s.charAt(i)-'a'+'A'));
            }else{
                sb.append(s.charAt(i));
            }
        }
        // 2.翻转整个字符串
        sb = sb.reverse();
        // 3.对于以空格为间隙的单词进行内部翻转
        for(int i=0;i<n;i++){
            // 利用j来寻找空格
            int j=i;
            while(j<n && sb.charAt(j)!=' '){
                j++;
            }
            String tmp = sb.substring(i,j);
            StringBuffer buffer = new StringBuffer(tmp);
            tmp = buffer.reverse().toString();
            sb.replace(i,j,tmp);
            i = j;
        }
        return sb.toString();
    }
}
